package token;

import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class getAccessToken {
    private static final String CORP_ID = "ww57280925e3a2ccd6";
    private static final String SECRET = "i90j61BPbB26I21L6gZylr8FdcoAqTc2ujKUXPuOcbg";
    public static String getAccessToken(){
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+CORP_ID+"&corpsecret="+SECRET;
        try {
            URL url1 = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
            urlConnection.setRequestMethod("GET");
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
      String line=null;
            StringBuilder builder=new StringBuilder();
            while ((line=bufferedReader.readLine())!=null){
                builder.append(line);
            }
            bufferedReader.close();
            JSONObject jsonObject=new JSONObject(builder.toString());
            return jsonObject.getString("access_token");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
